package com.hanggold.springboot.lesson4;


import com.hanggold.springboot.lesson4.annotation.EnableServer;
import com.hanggold.springboot.lesson4.annotation.EnableServerDefinition;
import com.hanggold.springboot.lesson4.annotation.EnableTest;
import com.hanggold.springboot.lesson4.annotation.EnableTy;
import com.hanggold.springboot.lesson4.bean.UserBean;
import com.hanggold.springboot.lesson4.enumo.ServerType;
import com.hanggold.springboot.lesson4.select.MyTest;
import com.hanggold.springboot.lesson4.service.Server;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.aop.support.AopUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;

import javax.servlet.ServletContainerInitializer;
import java.io.IOException;
import java.net.Proxy;
import java.util.Iterator;
import java.util.ServiceLoader;

//@EnableTy
//@EnableTest
@EnableServer(ServerType.HTTP)
//@MapperScan(basePackages="com.hanggold.springboot.lesson4.mybatis_test")
//@EnableServerDefinition(ServerType.FTP)
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

public class Lesson4Application {

    public static void main(String[] args) throws IOException {

//        MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory();
//
//        MetadataReader metadataReader =  metadataReaderFactory.getMetadataReader(Lesson4Application.class.getName());

//        ConfigurableApplicationContext configurableApplicationContext = new SpringApplicationBuilder(Lesson4Application.class).web(WebApplicationType.SERVLET).run(args);


        ConfigurableApplicationContext configurableApplicationContext =SpringApplication.run(Lesson4Application.class,args);


//        MyTest myTest =  configurableApplicationContext.getBean(MyTest.class);
//
//        System.out.println("MyTest: "+ myTest.getClass().getName());
//
//       UserBean userBean =  (UserBean) configurableApplicationContext.getBean("userBean");
//
//        System.out.println("username: "+ userBean.getClass().getName());
//
//       boolean b =  AopUtils.isAopProxy(myTest);
//        myTest.say("enable 模块驱动");


        Server server =  (Server)  configurableApplicationContext.getBean("http");

        server.start();


        init();
    }


    public static final void init() {
        ServiceLoader<ServletContainerInitializer> servletContainerInitializers = ServiceLoader.load(ServletContainerInitializer.class);


        Iterator<ServletContainerInitializer> servletContainerInitializerIterator = servletContainerInitializers.iterator();


        while (servletContainerInitializerIterator.hasNext()) {


            ServletContainerInitializer servletContainerInitializer = servletContainerInitializerIterator.next();


            System.out.println("ServletContainerInitializer: " + servletContainerInitializer.getClass().getName());

        }
    }

}
